package com.kbbywyclient.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.webkit.GeolocationPermissions;
import android.webkit.JavascriptInterface;
import android.webkit.JsPromptResult;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.kbbywyclient.R;
import com.kbbywyclient.app.API;
import com.kbbywyclient.di.AppComponent;
import com.kbbywyclient.ui.activity.base.BaseActivity;
import com.kbbywyclient.ui.helper.ShareHelper;
import com.kbbywyclient.util.pre.PreUtils;
import com.kbbywyclient.widget.CustomToolBar;

import butterknife.BindView;

/**
 * Created by Jiangzx on 2017/8/18 16:24.
 */

public class WebViewActivity extends BaseActivity {
    public static final String KEY_URL_S="url";
    public static final String KEY_TITLE_S="title";

    public static final String KEY_CARD_NAME ="cardName";

    public static final String KEY_SCREEN_NAME_S ="screenName";
    private String screenName;

    @BindView(R.id.v_toolbar)
    CustomToolBar toolBar;
    @BindView(R.id.progress_bar)
    ProgressBar progressBar;
    @BindView(R.id.webView)
    WebView webView;

    private String cardName;
    private ShareHelper shareHelper;

    private Handler handler;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_web_view;
    }

    @Override
    protected void initView() {
        WebSettings settings=webView.getSettings();
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        settings.setGeolocationEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setBlockNetworkLoads(false);
        settings.setDefaultTextEncodingName("UTF-8");
        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        webView.addJavascriptInterface(new JsInterface(), "jsInterface");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        webView.setWebChromeClient(new JWebChromeClient());
        webView.setWebViewClient(new JWebViewClient());
        toolBar.setLeftViewOnClickLinster(view->finish());

        handler=new Handler();
    }

    @Override
    public void initData() {
        Intent intent=getIntent();
        String url=intent.getStringExtra(KEY_URL_S);
        String title=intent.getStringExtra(KEY_TITLE_S);
        cardName=intent.getStringExtra(KEY_CARD_NAME);
        screenName=intent.getStringExtra(KEY_SCREEN_NAME_S);
        if(!TextUtils.isEmpty(title)){
            toolBar.setTitle(title);
        }
        if(!TextUtils.isEmpty(url)){
            webView.loadUrl(url);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(shareHelper!=null){
            shareHelper.onActivityResult(requestCode,resultCode,data);
        }
    }

    @Override
    public void initAppComponent(AppComponent appComponent) {

    }

    @Override
    protected void onResume() {
        super.onResume();
        if(!TextUtils.isEmpty(screenName)){
            setScreenName(screenName);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        webView.removeAllViews();
        webView.destroy();
        webView=null;
    }

    private class JWebViewClient extends WebViewClient{
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            return super.shouldOverrideUrlLoading(view, request);
        }
    }

    private class JWebChromeClient extends WebChromeClient{
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if(newProgress==100){
                progressBar.setVisibility(View.GONE);
            }else{
                if(progressBar.getVisibility()==View.GONE){
                    progressBar.setVisibility(View.VISIBLE);
                }
                progressBar.setProgress(newProgress);
            }
            super.onProgressChanged(view, newProgress);
        }

        @Override
        public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
            return super.onJsPrompt(view, url, message, defaultValue, result);
        }

        @Override
        public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
            callback.invoke(origin, true, false);
            super.onGeolocationPermissionsShowPrompt(origin, callback);
        }
    }

    private class JsInterface{
        @JavascriptInterface
        public void shareCard(String url,String platForm){
            Context context=WebViewActivity.this;
            if(shareHelper==null){
                shareHelper=new ShareHelper(WebViewActivity.this,"h5分享");
            }
            final ShareHelper.ShareBody shareBody =new ShareHelper.ShareBody();
            shareBody.url= API.WEB_ADDRESS+url;
            if(PreUtils.isAuthenticated(context)){
                shareBody.title="您的好友"+
                        PreUtils.getTrueName(context)+
                        "邀请您申请"+
                        (TextUtils.isEmpty(cardName)?"信用卡":cardName)+
                        ",优惠好礼享不停";
            }else{
                shareBody.title="您的好友邀请您申请"+
                        (TextUtils.isEmpty(cardName)?"信用卡":cardName)+
                        ",优惠好礼享不停";
            }
            try{
                shareBody.platForm=Integer.valueOf(platForm);
                shareBody.statisticsTitle=cardName;
                if(handler!=null){
                    shareHelper.share(shareBody);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
